/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/5/22.
//

#include "sql/stmt/update_stmt.h"
#include "sql/stmt/filter_stmt.h"
#include "common/log/log.h"
#include "storage/common/db.h"
#include "storage/common/field_meta.h"
#include "storage/common/table.h"

UpdateStmt::~UpdateStmt()
{
  if (nullptr != filter_stmt_) {
    delete filter_stmt_;
    filter_stmt_ = nullptr;
  }
}

UpdateStmt::UpdateStmt(Table *table, const Value *values, int value_amount, const char *attribute_name)
  : table_ (table), values_(values), attribute_name_(attribute_name), value_amount_(value_amount)
{}

RC UpdateStmt::create(Db *db, const Updates &update, Stmt *&stmt)
{
  // TODO
  const char *table_name = update.relation_name;
  if (nullptr == db || nullptr == table_name) {
    LOG_WARN("invalid argument. db=%p, table_name=%p", 
             db, table_name);
    return RC::INVALID_ARGUMENT;
  }

  // check whether the table exists
  Table *table = db->find_table(table_name);
  if (nullptr == table) {
    LOG_WARN("no such table. db=%s, table_name=%s", db->name(), table_name);
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }

  // get table map, currently only one table
  // std::unordered_map<std::string, Table *> table_map;
  // table_map.insert(std::pair<std::string, Table*>(table_name, table));

  // check whether the fields exists
  const FieldMeta *field_meta = nullptr;
  const TableMeta &table_meta = table->table_meta();
  for (int i = 0; i < table_meta.field_num(); i ++) {
    if (std::string(table_meta.field(i)->name()) == std::string(update.attribute_name)) {
      field_meta = table_meta.field(i);
      break;
    }
  }
  if (field_meta == nullptr) {
    LOG_WARN("no such field. db=%p, table_name=%p, field_name=%p",
             db, table_name, update.attribute_name);
    return RC::SCHEMA_FIELD_MISSING;
  }

  // check fields type
  if (field_meta->type() != update.value.type) {
      LOG_WARN("field type mismatch. table=%s, field=%s, field type=%d, value_type=%d", 
               table_name, field_meta->name(), field_meta->type(), update.value.type);
      return RC::SCHEMA_FIELD_TYPE_MISMATCH;
  }

  // create filter statement in 'where' statement
  FilterStmt *filter_stmt = nullptr;
  RC rc = FilterStmt::create(db, table, nullptr,
           update.conditions, update.condition_num, filter_stmt);
  if (rc != RC::SUCCESS) {
    LOG_WARN("cannot construct filter stmt");
    return rc;
  }

  // everything alright
  UpdateStmt *update_stmt = new UpdateStmt(table, &update.value, 1, update.attribute_name);
  update_stmt->filter_stmt_ = filter_stmt;
  stmt = update_stmt;
  return RC::SUCCESS;
}
